package web.Servlet;

import domain.news;
import service.Serviceimpl.newsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(name = "NewsServlet" ,urlPatterns = "/hbcs")
public class NewsServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("refresh","5;url=/bb.jsp");
        newsService newsService = new newsService();
        List<news> all = newsService.findAll();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        for(int i=0;i<all.size();i++){
            Date create_time = all.get(i).getCreate_time();
            String format = simpleDateFormat.format(create_time);
            all.get(i).setTime(format);
        }
        req.getSession().setAttribute("news",all);
       req.getRequestDispatcher("bb.jsp").forward(req,resp);
    }
}
